package cn.echo.modules.sys.service;

import cn.echo.modules.sys.dao.ArticleMapper;
import cn.echo.modules.sys.dao.SiteMapper;
import cn.echo.modules.sys.entity.Article;
import cn.echo.modules.sys.entity.User;
import cn.echo.modules.sys.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/4/12.
 */
@Service
@Transactional
public class ArticleService {
//    @Autowired
//    private SqlSession sqlSession;
    @Autowired
    private ArticleMapper articleMapper;
    public void add(Article article) {
        try {
            articleMapper.add(article);
        } catch (Exception e) {
            System.out.println("添加失败!");
            e.printStackTrace();
        }
    }

    public void delete(Article article) {
        try {
            articleMapper.remove(article);
        } catch (Exception e) {
            System.out.println("删除失败!");
            e.printStackTrace();
        }
    }

    public List<Article> findAll(Article article) {
        List<Article> articleList = null;
        try {
            articleList  = articleMapper.findAll(new Article());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return articleList;



    }

    public List<Article> findAllById(User user) {
        List<Article> articleList = null;
        try {
            articleList = articleMapper.findAllById(user);
        } catch (Exception e) {
            System.out.println("查询失败！");
            e.printStackTrace();
        }
        return articleList;
    }

    public void update(Article article) {
        try {
            articleMapper.edit(article);
        } catch (Exception e) {
            System.out.println("更新失败");
            e.printStackTrace();
        }

    }

    public Article findById(String id) {


        Article article = null;
        List<Article> articleList = null;
        try {
            article = articleMapper.findById(id);
        } catch (Exception e) {
            System.out.println("查询失败");
            e.printStackTrace();
        }
        return article;
    }

    public List<Article> serachArticle(String title) {


        List<Article> artilcleList = null;
        try {
            artilcleList = articleMapper.findLike(title);
        } catch (Exception e) {
            System.out.println("搜索失败！");
            e.printStackTrace();
        }
        return artilcleList;
    }
}
